package com.sskj.givecoin.v.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.sskj.givecoin.R;
import com.sskj.givecoin.common.ClassicsHeader;
import com.sskj.givecoin.common.adapter.BaseAdapter;
import com.sskj.givecoin.common.adapter.ViewHolder;
import com.sskj.givecoin.common.widget.ToolBarLayout;
import com.sskj.givecoin.m.bean.ShareEntry;
import com.sskj.givecoin.p.activity.ShareDetailPresenter;
import com.sskj.givecoin.utils.TimeFormatUtil;
import com.sskj.givecoin.v.base.BaseActivity;

import butterknife.BindView;

/**
 * 推广明细
 */
public class ShareDetailActivity extends BaseActivity<ShareDetailPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.share_total)
    TextView shareTotal;
    @BindView(R.id.share_new)
    TextView shareNew;
    @BindView(R.id.share_detail_list)
    RecyclerView shareDetailList;
    @BindView(R.id.share_refresh)
    SmartRefreshLayout shareRefresh;

    private int page = 0;
    private BaseAdapter<ShareEntry.Data> adapter;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_share_detail;
    }

    @Override
    public ShareDetailPresenter getPresenter() {
        return new ShareDetailPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setLeftButtonOnClickLinster(v -> finish());
        shareRefresh.setRefreshHeader(new ClassicsHeader(this));
        shareRefresh.setRefreshFooter(new ClassicsFooter(this));
        shareRefresh.setOnRefreshListener(v -> {
            page = 0;
            mPresenter.getData(page);
        });
        shareRefresh.setOnLoadMoreListener(v -> {
            page++;
            mPresenter.getData(page);
        });
        shareDetailList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<ShareEntry.Data>(R.layout.item_share, null, shareDetailList,true) {
            @Override
            public void bind(ViewHolder holder, ShareEntry.Data item) {
                holder.setText(R.id.name, item.getRealname());
                if (!TextUtils.isEmpty(item.getMail())) {
                    holder.setText(R.id.account, item.getMail());
                }
                if (!TextUtils.isEmpty(item.getMobile())) {
                    holder.setText(R.id.account, item.getMobile());
                }
                holder.setText(R.id.time, TimeFormatUtil.SF_FORMAT_E.format(item.getReg_time() * 1000));
            }
        };

    }

    @Override
    protected void initData() {
        mPresenter.getData(page);
    }


    public void setData(ShareEntry data) {
        setText(shareTotal, data.getTtl());
        setText(shareNew, data.getToday());
        if (page == 0) {
            adapter.setNewData(data.getList());
        } else {
            adapter.addData(data.getList());
        }
    }

    public void stopRefresh() {
        shareRefresh.finishRefresh();
        shareRefresh.finishLoadMore();
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, ShareDetailActivity.class);
        context.startActivity(intent);
    }
}
